
import server_url from './config.js'

let token = ''  //凭证
function request(options = {}) {
       // store.state.token && (token = store.state.token); //从vuex中获取登录凭证
	   token = uni.getStorageSync('token')
       options.url = `${server_url}${options.url}`;
          //配置请求头
        options.header = {
        // 'content-type': 'application/x-www-form-urlencoded',
        'token': `${token}` //Bearer 
    };
    return new Promise((resolved, rejected) => {
        options.success = (res) => {
			console.log(res)
            if (res.data.code == 200) {  //请求成功
                resolved(res.data);
            } else {
                uni.showToast({
                    icon: 'none',   
                    duration: 3000,
                    title: `${res.data.msg}`
                });
                rejected(res.data.msg);//错误
            }
        }
              //错误
        options.fail = (err) => {
            rejected(err); //错误
        }
        uni.request(options);
    });
}
export default request;
